//Get方法并连接SQLServer数据库获取数据，以Json字符串格式返回：
 
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Http;
using Newtonsoft.Json;
 
namespace WebApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
 
        // GET api/values/5
        public string Get(int id)
        {
            try
            {
                SqlConnection sqlConnection =
                    new SqlConnection(
                        "Data Source=127.0.0.1;Initial Catalog=GaryWeb;Integrated Security=True;User Id=sa;Password=123456");
                sqlConnection.Open();
                string sql = "select * from Users";
                DataSet dataSet = new DataSet();
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
                sqlDataAdapter.Fill(dataSet);
                return JsonConvert.SerializeObject(dataSet);
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
 
        // POST api/values
        public void Post([FromBody]string value)
        {
        }
 
        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }
 
        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}